package com.wanmait.shoppingsys.controller.manage;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wanmait.shoppingsys.dao.AdminDAO;
import com.wanmait.shoppingsys.vo.Admin;

@WebServlet("/AdministratorController")
public class AdministratorController extends HttpServlet{

	private AdminDAO adminDAO=AdminDAO.getDAO();
	

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action=request.getParameter("action");
		if(action==null) return;
		switch(action)
		{
		case "reg":
			this.reg(request,response);
			break;
		case "login":
			this.login(request,response);
			break;
		case "logout":
			this.logout(request,response);
			break;
		case "findpassword":
			this.findpassword(request,response);
		case "alter":
			this.alter(request,response);
			break;
		}
	}
	
	protected void alter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String path=request.getServletContext().getContextPath();
		String pass=request.getParameter("pass");
		String id=request.getParameter("id");
		Integer i=Integer.parseInt(id);
		adminDAO.updatePass(i, pass);
		response.sendRedirect(path+"/administrator/login.jsp?err=3");
	}
	
	protected void findpassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path=request.getServletContext().getContextPath();
		
		String name=request.getParameter("name");
		String realname=request.getParameter("realname");
		
		Admin admin=adminDAO.findpass(name,realname);
		int id=admin.getId();
		if(admin==null) {
			response.sendRedirect(path+"/administrator/findpass.jsp?err=1");
		}
		if(admin!=null) {
			response.sendRedirect(path+"/administrator/alter.jsp?id="+id);
		}
	}
	
	protected void reg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path=request.getServletContext().getContextPath();
		String name=request.getParameter("name");
		String pass=request.getParameter("pass");
		String realname=request.getParameter("realname");
		
		Admin admin=new Admin();
		admin.setName(name);
		admin.setPass(pass);
		admin.setRealName(realname);
		
		adminDAO.insert(admin);
		response.sendRedirect(path+"/administrator/login.jsp");
	}
	
	protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path=request.getServletContext().getContextPath();
		String name=request.getParameter("name");
		String pass=request.getParameter("pass");
		Admin admin=adminDAO.findByNameAndPass(name,pass);
		if(admin==null) {
			response.sendRedirect(path+"/administrator/login.jsp?err=1");
		}
		else {
		if(admin.isEnable()==false) {
			response.sendRedirect(path+"/administrator/login.jsp?err=2");
		}
		else {
			HttpSession session=request.getSession();
			session.setAttribute("admin", admin);
			response.sendRedirect("/ShoppingSys/manage.jsp");
		}
		}
	}
	protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path=request.getServletContext().getContextPath();
		request.getSession().removeAttribute("admin");
		response.sendRedirect(path+"administrator/login.jsp");
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
